import numpy as np
import matplotlib.pyplot as plt
def plot_bode(magnitude, amplitude, phase, phase180=True):
    amplitude = 20*np.log10(amplitude)
    fig = plt.figure()
    fig.subplots_adjust(hspace=0.1, top=0.99, bottom=0.05, left=0.06, right=0.99)
    ax1 = fig.add_subplot(211)
    ax1.grid(True)
    ax1.plot(magnitude, amplitude)
    ax2 = fig.add_subplot(212)
    ax2.plot(magnitude, phase)
    if phase180:
        ax2.set_yticks(np.linspace(180, -360, 13))
    ax2.set_ylim(phase.min()-10, phase.max()+10)
    ax2.grid(True)
    plt.show()
if __name__ == '__main__':
    index = np.linspace(-2, 3, 41)
    omega = np.power(10, index)
    phi = -(np.pi/2+np.arctan2(0.1*omega, 1)+np.arctan2(0.2*omega, 1))*180/np.pi
    amp = 30/(omega*np.sqrt(1+(0.1*omega)**2)*np.sqrt(1+(0.2*omega)**2))
    amp = 20*np.log10(amp)
    plot_bode(index, amp, phi)